﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CNKI.TPI.Web.Base
{
    public static class SqlBuilder
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="select"></param>
        /// <param name="table"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public static IList<string> Build(string select, Dictionary<String, String> table, string where = null)
        {
            IList<string> sqlList = new List<string>();
            string sql = @"select {0} from {1}";
            string temp = "";
            try
            {
                foreach (string item in table.Keys)
                {
                    temp = string.Format(sql, select, table[item]);
                    if (!string.IsNullOrEmpty(where))
                    {
                        temp += " where " + where;
                    }
                    sqlList.Add(temp);
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return sqlList;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="select"></param>
        /// <param name="table"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public static IList<string> Build(IList<string> select, Dictionary<String, String> table, string where = null)
        {
            if (null == select || null == table || null == where)
            {
                return new List<string>();
            }
            string columns = "";
            foreach (string item in select)
            {
                columns += item + ",";
            }
            return Build(columns.TrimEnd(','), table, where);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="select"></param>
        /// <param name="table"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public static string Build(IList<string> select, string table, string where = null)
        {
            if (null == select || null == table || null == where)
            {
                return "";
            }
            string columns = "";
            foreach (string item in select)
            {
                columns += item + ",";
            }
            return Build(columns.TrimEnd(','), table, where);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="select"></param>
        /// <param name="table"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public static string Build(string select, String table, string where = null)
        {
            string result = "";
            string sql = @"select {0} from {1}";
            try
            {
                result = string.Format(sql, select, table);
                if (!string.IsNullOrEmpty(where))
                {
                    result += " where " + where;
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
    }
}
